<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Ausentes extends CI_Controller {

	function __construct() {
		parent::__construct();
		if(!$this->session->userdata('disproju')){
			$this->session->sess_destroy();
			redirect('login', 'refresh');
		}
	}
	
	public function index(){
		$this->load->view('v_usuario_consultas');
	}
	public function novo($id = null){
		//******************************************************
		//   PERMISSAO 
		//******************************************************
		$session_data = $this->session->userdata('disproju');
		temPermissao(array('1','2'),$session_data);
		
		$p = new Usuario();
		$p->get_by_id($id);
		if( ! $p->exists())
		{
			show_error('Invalid User ID');
		}else{
			$data['ausente'] = $p;
			$this->load->view('header');
			$this->load->view('v_ausente',$data);
		}
	}
	public function salvar(){
	
		//******************************************************
		//   PERMISSAO 
		//******************************************************
		$session_data = $this->session->userdata('disproju');
		temPermissao(array('1','2'),$session_data);
				
		$u = new Ausente();
		if($this->input->post()){
			$u->usuario_id =  $this->input->post('id');
			$u->dt_inicio = brToUsa($this->input->post('inicio'));
			$u->dt_fim = brToUsa($this->input->post('fim'));
			$u->tipo = strtoupper($this->input->post('tipo'));
			$u->observacao = strtoupper($this->input->post('observacao'));

			/* $existe = new Ausente();
			$existe->where("usuario_id",$this->input->post('id'))
			->where_between(brToUsa($this->input->post('inicio')),"dt_inicio","dt_fim")
			->or_where_between(brToUsa($this->input->post('fim')),"dt_inicio","dt_fim")
			->get();
			echo $existe->check_last_query(); */
			
			$existe = new Ausente();
			$aux = new Ausente();
			$aux = $existe->existeFerias($this->input->post('id'),brToUsa($this->input->post('inicio')),brToUsa($this->input->post('fim')));
									
			if(!$aux->exists()){
				if($u->save()){
				
					$usuario = new Usuario();
					$usuario->where("id", $this->input->post('id') )->get();
					$usuario->ausente_id_atual = $u->id;
					//echo 'AAAAAAAAAAAAA: '.$u->id;
					$usuario->save();
				
					$acao = $u->check_last_query(false,true);
					$log = new Log();
					$log->ip = $this->session->userdata('ip_address');
					$log->usuario = $session_data["nome"];
					$log->acao = $acao;
					$log->dt_log = date('Y-m-d H:i:s');
					$log->save();
					echo 'OK';
				}else{

					$acao = $u->error->string;
					$log = new Log();
					$log->ip = $this->session->userdata('ip_address');
					$log->usuario = $session_data["nome"];
					$log->acao = $acao;
					$log->dt_log = date('Y-m-d H:i:s');
					$log->save();
					
					echo $u->error->string;
				}
			}else{
			
				echo '<div class="danger alert-danger" role="danger">Usuário possui férias cadastradas no período: '.usaToBr($aux->dt_inicio).' a '.usaToBr($aux->dt_fim).'</div>';
			}
		}
		
	}
	public function editar(){
	
		//******************************************************
		//   PERMISSAO 
		//******************************************************
		$session_data = $this->session->userdata('disproju');
		temPermissao(array('1','2'),$session_data);
		
		$this->load->view('header');
		
		$id =  $this->input->post('id');
		
		$p = new Usuario();
		$p->where('id', $id)->get();
		
		$p->login =  strtoupper($this->input->post('login'));
		$p->nome_completo =  strtoupper($this->input->post('nome'));
		$p->permissao_id = $this->input->post('permissao');
		$p->status = strtoupper($this->input->post('status'));
		$p->observacao = strtoupper($this->input->post('observacao'));
		
		//print_r($p);
		
		if($p->save()){
			$acao = $p->check_last_query(false,true);
			$log = new Log();
			$log->ip = $this->session->userdata('ip_address');
			$log->usuario = $session_data["nome"];
			$log->acao = $acao;
			$log->dt_log = date('Y-m-d H:i:s');
			$log->save();
			redirect('usuarios/consulta');
		}else{
			$acao = $p->error->string;
			$log = new Log();
			$log->ip = $this->session->userdata('ip_address');
			$log->usuario = $session_data["nome"];
			$log->acao = $acao;
			$log->dt_log = date('Y-m-d H:i:s');
			$log->save();
			echo $p->error->string;
		}
	}
	public function consultaFeriasUsuario($id = null){
		$a = new Ausente();
		$a->where("usuario_id", $id)->order_by('id desc')->limit(1)->get();
		echo $a->tipo;
		if($a->exists())
		{
			//$msg=
			return $a;
		}
	}
	public function consulta(){
		//******************************************************
		//   PERMISSAO 
		//******************************************************
		$session_data = $this->session->userdata('disproju');
		temPermissao(array('1','2'),$session_data);
		
		$ausente = new Ausente();
		$aux = new Ausente();
		$aux = $ausente->consultaFerias();
		
		$data["ausentes"] = $aux;
		$data["session_data"] = $session_data;
		$this->load->view('header');
		$this->load->view('v_ausente_consulta',$data);
	}
	public function info($id = NULL){
		$this->load->view('header');
		if( ! empty($id))
		{
			$p = new Usuario();
			$p->get_by_id($id);
			if( ! $p->exists())
			{
				show_error('Invalid User ID');
			}
		}

		$data['usuario'] = $p;
		
		$perm = new Permissao();
		$data['permissao'] = $perm->consultaPermissao('3');
		$this->load->view('v_usuario_info',$data);
	}

}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */